package com.originskyseed.platform.count.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import com.originskyseed.platform.core.base.BasePage;
import com.originskyseed.platform.core.tenant.util.TenantCodeUtil;
import com.originskyseed.platform.core.util.bean.PrimaryKeyGeneratorUtil;
import com.originskyseed.platform.count.mapper.UserAchievementMapper;
import com.originskyseed.platform.count.model.UserAchievement;
import com.originskyseed.platform.count.service.IUserAchievementService;

/**
 * 用户业绩信息表Service接口实现类（OSI_USER_ACHIEVEMENT）
 * 
 * @author Tangwanghui
 */
@DubboService
@Component("userAchievementService")
public class UserAchievementServiceImpl implements IUserAchievementService {

	@Resource
	private UserAchievementMapper userAchievementMapper;

	/**
	 * 删除记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKey(String id) throws Exception {
		return this.userAchievementMapper.deleteByPrimaryKey(id);
	}

	/**
	 * 批量删除记录
	 * 
	 * @param ids
	 * @return
	 * @throws Exception
	 */
	@Override
	public int deleteByPrimaryKeys(List<String> ids) throws Exception {
		return this.userAchievementMapper.deleteByPrimaryKeys(ids);
	}

	/**
	 * 插入记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insert(UserAchievement record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userAchievementMapper.insert(record);
	}

	/**
	 * 插入记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int insertSelective(UserAchievement record) throws Exception {
		if (StringUtils.isBlank(record.getId())) {
			record.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
		}
		record.setCreateTime(new Date());
		return this.userAchievementMapper.insertSelective(record);
	}

	/**
	 * 更新记录
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKey(UserAchievement record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userAchievementMapper.updateByPrimaryKey(record);
	}

	/**
	 * 更新记录（仅非空值字段）
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public int updateByPrimaryKeySelective(UserAchievement record) throws Exception {
		record.setUpdateTime(new Date());
		return this.userAchievementMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * 查询记录
	 * 
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public UserAchievement selectByPrimaryKey(String id) throws Exception {
		return this.userAchievementMapper.selectByPrimaryKey(id);
	}

	/**
	 * 查询列表
	 * 
	 * @param record
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<UserAchievement> selectList(UserAchievement record) throws Exception {
		return this.userAchievementMapper.selectList(record);
	}

	/**
	 * 分页查询列表
	 * 
	 * @param params
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@Override
	public BasePage<UserAchievement> selectListByPage(Map<String, Object> params, BasePage<UserAchievement> page) throws Exception {
		params.put("page", page);
		List<UserAchievement> list = this.userAchievementMapper.selectListByPage(params);
		page.setResults(list);
		return page;
	}

	/**
	 * 统计员工每日业绩
	 * 
	 * @param date
	 * @param tenantCode
	 * @return
	 * @throws Exception
	 */
	public int insertsUserBonusByTime(String date, String tenantCode) throws Exception {
		if (StringUtils.isNotEmpty(tenantCode)) {
			TenantCodeUtil.getInstance().createServerTenantCode(tenantCode);
		}
		List<UserAchievement> list = this.userAchievementMapper.selectUserAchievementByTime(date);

		for (UserAchievement userAchievement : list) {
			userAchievement.setId(PrimaryKeyGeneratorUtil.getInstance().getRandomKey());
			userAchievement.setDate(date);
			userAchievement.setCreateTime(new Date());
		}

		int count = 0;
		// 一次性插入的数量
		int size = 500;
		// 总长度
		int length = list.size();
		// 计算可以分成多少组
		int num = (length + size - 1) / size;
		for (int i = 0; i < num; i++) {
			// 开始位置
			int fromIndex = i * size;
			// 结束位置
			int toIndex = (i + 1) * size < length ? (i + 1) * size : length;
			List<UserAchievement> newUserAchievement = list.subList(fromIndex, toIndex);
			count += userAchievementMapper.insertBatch(newUserAchievement);
		}
		return count;
	}
}